<template>
    <div>
        <p id="qrCode" ref="qrCodeDiv" class="margin-top2"></p>
        <div class="margin-top3 pointer flexcenter">
            <el-button v-if="type!=='pay'" type="text" @click="bindQRCode">刷新二维码</el-button>
            <el-button v-if="type=='pay'" type="text" @click="close">刷新二维码</el-button>
        </div>
    </div>
</template>

<script>
import {searchRealNameRes} from '@/api/userinfo'
import QRCode from 'qrcodejs2';

export default {
    name: "Scancertificat",
    props: {
        url: "",
        type:'',
    },
    data(){
        return {
            timer:null
        }
    },
    mounted: function () {
        this.$nextTick(function () {
            this.bindQRCode();
        })
        if(this.type=='pay'){
            this.timer=setInterval(()=>{
                searchRealNameRes().then(res=>{
                    if(res.result==1){
                        this.clearTimer()
                        this.$message.success('实名认证成功')
                        this.$router.push({
                            path:"/admin/userInfo"
                        })
                        this.close()
                    }
                })
            },2500)
        }
        
    },
    beforeDestroy(){
        this.clearTimer()
    },
    methods: {
        close(){
            this.bindQRCode()
        },
        bindQRCode: function () {
            this.$refs.qrCodeDiv.innerHTML = ""//词句代码将之前的二维码清空了
            new QRCode(this.$refs.qrCodeDiv, {
                text: this.url,//后台返回的链接
                width: 200,
                height: 200,
                colorDark: "#333333", //二维码颜色
                colorLight: "#ffffff", //二维码背景色
                correctLevel: QRCode.CorrectLevel.L//容错率，L/M/H
            })
        },
        clearTimer(){
            clearInterval(this.timer)
            this.timer = null
        },
    }
}
</script>

<style></style>